package com.example.swaggerDemo.controller.comoon;

import com.example.swaggerDemo.entity.MsgEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;



/**
 * @Description
 * @Author sgl
 * @Date 2019-05-13 14:04
 */
@Api("文件上传接口")
@RestController
@RequestMapping(value="/Upload")

public class UploadController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);
    @Autowired
    private Environment env;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

    @ApiOperation(value="单文件上传",notes="单文件上传")
    @ResponseBody
    @RequestMapping(value= "/multiUploadss",method=RequestMethod.POST)
    public MsgEntity upload(HttpServletRequest request) {
        MsgEntity msg=new MsgEntity();
        MultipartFile  file = ((MultipartHttpServletRequest) request).getFile("file");
        if (file.isEmpty()) {
            msg.setmsg("上传失败，请您重新选择文件！");
            msg.setcode(false);
            return msg;
        }
        UUID uuid = UUID.randomUUID();
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        fileName =uuid+ "."+"png";
        String filePath =env.getProperty("upload.filePath");
        File f = new File(filePath);
        File fileParent = f.getParentFile();
        if(!fileParent.exists()){
            fileParent.mkdirs();
        }
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            msg.setmsg("上传成功");
            msg.setcode(true);
            msg.setobject("/upload/" + fileName);
        } catch (Exception e) {
            msg.setmsg(e.getMessage());
            msg.setcode(false);
            System.out.println(e.getMessage());
        }
        return msg;
    }

    @ApiOperation(value="多文件上传",notes="多文件上传")
    @ResponseBody
    @RequestMapping(value="/multiUpload",method=RequestMethod.POST)
    public MsgEntity multiUpload(HttpServletRequest request) {
        MsgEntity msg=new MsgEntity();
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        UUID uuid = UUID.randomUUID();
        String filePath =env.getProperty("upload.filePath");
        String fileName="";
        File f = new File(filePath);
        File fileParent = f.getParentFile();
        if(!fileParent.exists()){
            fileParent.mkdirs();
        }
        for (int i = 0; i < files.size(); i++) {
            MultipartFile file = files.get(i);
            if (file.isEmpty()) {
                msg.setmsg("上传失败，请您重新选择文件！");
                msg.setcode(false);
                return msg;
            }
             fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            fileName =uuid+ ".png";
            File dest = new File(filePath + fileName);
            try {
                file.transferTo(dest);
                msg.setmsg("上传成功");
                msg.setcode(true);
                msg.setobject("/upload/" + fileName);
                System.out.println(df.format(new Date())+"上传成功："+ file.getSize());
            } catch (Exception e) {
                msg.setmsg(e.getMessage());
                msg.setcode(false);
                System.out.println(df.format(new Date())+"上传失败："+ file.getSize());
                System.out.println(e.getMessage());
            }
        }
        return msg;
    }
}
